#include "include/hyn_core.h"
// #include "freertos/FreeRTOS.h"
#include "stm32h7xx_hal.h"


#define HYN_TOUCH_INT_IO  (uint64_t)(((uint64_t)GPIOB << 32) | GPIO_PIN_15)
#define HYN_TOUCH_RSET_IO (uint64_t)(((uint64_t)GPIOB << 32) | GPIO_PIN_14)

static void MX_GPIO_Init(void);


struct hyn_ts_data* hyn_data = NULL;

void hyn_delay_ms(int cnt)
{
    // vTaskDelay(pdMS_TO_TICKS(cnt));
    HAL_Delay(cnt);
}

int touch_int_handler(void)
{
    int ret;
    hyn_data->hyn_irq_flg = 1;
    if(hyn_data->work_mode < DIFF_MODE)
    {
        u32 x_res = hyn_data->plat_data.x_resolution;
        u32 y_res = hyn_data->plat_data.y_resolution;

        ret = hyn_data->hyn_fuc_used->tp_report();    // 读点

        for(u8 i = 0; i < hyn_data->rp_buf.rep_num; i++)
        {    // 根据配置修改坐标原点
            if(hyn_data->plat_data.swap_xy)
            {
                u16 tmp                            = hyn_data->rp_buf.pos_info[i].pos_x;
                hyn_data->rp_buf.pos_info[i].pos_x = hyn_data->rp_buf.pos_info[i].pos_y;
                hyn_data->rp_buf.pos_info[i].pos_y = tmp;
                x_res                              = hyn_data->plat_data.y_resolution;
                y_res                              = hyn_data->plat_data.x_resolution;
            }
            if(hyn_data->plat_data.reverse_x)
                hyn_data->rp_buf.pos_info[i].pos_x = x_res - hyn_data->rp_buf.pos_info[i].pos_x;
            if(hyn_data->plat_data.reverse_y)
                hyn_data->rp_buf.pos_info[i].pos_y = y_res - hyn_data->rp_buf.pos_info[i].pos_y;
        }
        printf("ret:%d num:%d xy:(%d,%d)", ret, hyn_data->rp_buf.rep_num, hyn_data->rp_buf.pos_info[0].pos_x, hyn_data->rp_buf.pos_info[0].pos_y);
    }
    hyn_data->rp_buf.report_need = REPORT_NONE;
    return 0;
}

void touch_init()
{
    int                       ret = 0;
    static struct hyn_ts_data ts_data;
    memset((void*)&ts_data, 0, sizeof(ts_data));
    hyn_data = &ts_data;
    HYN_INFO(HYN_DRIVER_VERSION);
    /*************************************************************/
    //    handle            chip types
    // &cst66xx_fuc   /*suport 36xx 35xx 66xx 68xx 148E*/
    // &cst3240_fuc   /*suport 3240 */
    // &cst92xx_fuc   /*suport 9217、9220 */
    // &cst3xx_fuc    /*suport 328 128 140 148 340 348*/
    // &cst7xx_fuc    /*suport 726 826 830 836u*/
    // &cst8xxT_fuc   /*suport 816t 816d 820 08C*/
    // &cst226se_fuc  /*226se 8922*/
    /*************************************************************/
    hyn_data->hyn_fuc_used            = &cst8xxT_fuc;         // 根据芯片型号赋值
    hyn_data->plat_data.max_touch_num = MAX_POINTS_REPORT;    // 最大手指数
    hyn_data->plat_data.x_resolution  = 320;                  // x最大分辨率
    hyn_data->plat_data.y_resolution  = 386;                  // y最大分辨率
    hyn_data->plat_data.swap_xy       = 1;                    // xy坐标交换
    hyn_data->plat_data.reverse_x     = 0;                    // x坐标反向
    hyn_data->plat_data.reverse_y     = 1;                    // y坐标反向

    hyn_data->plat_data.irq_gpio   = HYN_TOUCH_INT_IO;
    hyn_data->plat_data.reset_gpio = HYN_TOUCH_RSET_IO;


    // gpio_config_t io_conf = {};
    // //disable interrupt
    // io_conf.intr_type = GPIO_INTR_DISABLE;
    // //set as output mode
    // io_conf.mode = GPIO_MODE_OUTPUT;
    // //bit mask of the pins that you want to set,e.g.GPIO18/19
    // io_conf.pin_bit_mask = (uint64_t)1<<HYN_TOUCH_RSET_IO;
    // //disable pull-down mode
    // io_conf.pull_down_en = 0;
    // //disable pull-up mode
    // io_conf.pull_up_en = 1;
    // //configure GPIO with the given settings
    // gpio_config(&io_conf);

    // gpio_set_level(HYN_TOUCH_RSET_IO,0);
    // vTaskDelay(pdMS_TO_TICKS(100));
    // gpio_set_level(HYN_TOUCH_RSET_IO,1);
    // vTaskDelay(pdMS_TO_TICKS(100));

    // io_conf.mode = GPIO_MODE_INPUT;
    // io_conf.pin_bit_mask = (uint64_t)1<<HYN_TOUCH_INT_IO;
    // gpio_config(&io_conf);

    // IO init
    MX_GPIO_Init();

    touch_iic_init();

    ret = hyn_data->hyn_fuc_used->tp_chip_init(hyn_data);    // 初始化
    if(ret)
    {
        HYN_ERROR("I2c NAk");
        return;
    }
    HYN_INFO("IC_info fw_project_id:%04x ictype:%04x fw_ver:%x", hyn_data->hw_info.fw_project_id, hyn_data->hw_info.fw_chip_type, hyn_data->hw_info.fw_ver);

#if HYN_POWER_ON_UPDATA
    extern unsigned char cst_816t_app_bin[];
    if(ts_data.need_updata_fw)
    {
        hyn_data->fw_file_name[0] = 0;    // use .h to updata
        hyn_data->hyn_fuc_used->tp_updata_fw(cst_816t_app_bin, 15366);
    }
#endif
    // regest irq
    // io_ext_interrupt_init(hyn_data->plat_data.irq_gpio, falling_edge_trigger, touch_int_handler);
}



static void MX_GPIO_Init(void)
{
		GPIO_InitTypeDef GPIO_InitStruct = {0};
		/* USER CODE BEGIN MX_GPIO_Init_1 */
		/* USER CODE END MX_GPIO_Init_1 */

		/* GPIO Ports Clock Enable */
		__HAL_RCC_GPIOB_CLK_ENABLE();


		/*Configure GPIO pin Output Level */
		HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);


		/*Configure GPIO pins : PB12 PB14 */
		GPIO_InitStruct.Pin   = GPIO_PIN_14;
		GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
		GPIO_InitStruct.Pull  = GPIO_NOPULL;
		GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
		HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

		/*Configure GPIO pin : PB15 */
		GPIO_InitStruct.Pin  = GPIO_PIN_15;
		GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
		GPIO_InitStruct.Pull = GPIO_PULLUP;
		HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

		/* USER CODE BEGIN MX_GPIO_Init_2 */
		/* USER CODE END MX_GPIO_Init_2 */
}
